<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>找回密码</title>
</head>
<script th:src="@{/js/jquery.js}"></script>
<script>
    function nameRe(){
        var yn=false;
        var name=$("input[name='name']");
        if(name.val()!=null&&name.val()!=''){
            var params="name="+name.val();
            $.ajax({
                url:"/user/selUserName",
                type:"get",
                async:false,
                data:params,
                success:function (data) {
                    /*alert(data);*/
                    if(data==1){
                        name.next("span").html("");
                        yn=true;
                        // return false;
                    }else{
                        name.next("span").html("用户名不存在请重新输入");

                        // return true;
                    }
                }
            });
        }else{
            name.next("span").html("请输入用户名");
        }
        return yn;
    }
    //邮箱非空验证
    function emailRe(){
        var email=$("input[name=email]");
        if(email.val()!=null&&email.val()!=''){
            email.next("span").html("");
            return true;
        }else {
            email.next("span").html("邮箱不能为空");
            return  false;
        }
    }
    //验证码验证
    function verifyRe() {
        var yn=false;
        var verify=$("input[name=verify]");
        if(verify.val()!=null&&verify.val()!=''){
            if(verifyCode==-1){
                verify.next("input").next("span").html("请先获取验证码");
            }else{
                if(verifyCode!=verify.val()){
                    verify.next("input").next("span").html("验证码输入错误");
                }else{
                    yn=true;
                    verify.next("input").next("span").html("");
                }
            }
        }else{
            verify.next("input").next("span").html("请先输入验证码");
        }
        return yn;
    }

    //验证码
    var verifyCode=-1;
    function showHide() {
        $("#upPwdName").toggle();
        $("#upPwdEmail").toggle();
    }
    $(function () {
        //绑定用户名事件
        $("input[name='name']").blur(nameRe);
        //用户名修改密码提交事件   改成局部刷新
        $("#upPwdName").submit(function () {
           if(nameRe()){
               return true;
           } else {
               return false;
           }
        });
        /*$("#obtain").click(function () {
            var params="name="+$("input[name='name']").val();
            /!*alert(nameRe());*!/
            if(nameRe()){
                $.post("/user/upPwdName",params,function (data) {
                    alert(data);
                },"json") ;
            }
        });*/

////////////////////////////////////////////////////////////////////////////////

        //验证码验证
        $("input[name=verify]").blur(verifyRe);
        //邮箱非空验证
        $("input[name=email]").blur(emailRe);
        //点击获取验证码
        $("#verificationCode").click(function () {
            alert("verificationCode");
            var email=$("input[name=email]");
            if(email.val()!=null&&email.val()!=''){
                $.getJSON("/user/verificationCode","email="+email.val(),function (data) {
                    /*alert(data);*/
                    //赋值验证码
                    verifyCode=data;
                });
            }else{
                emailRe();
            }
        });
        //邮箱修改密码 提交事件
        $("#upPwdEmail").submit(function () {
            if(emailRe()&&verifyRe()){
                return true;
            }else{
                return false;
            }
        });


        /////////////////////////////////////////////////////////////
        //回车  制表符事件    当我在后台没登入成功后这玩意已经不管用
        $(window).keydown(function (event) {
            if(event.keyCode==9){
                showHide();
            }else if(event.keyCode==13){
                $("form:visible").submit();
            }
        });
    });
</script>
<body>
<h3>找回密码</h3>
<form id="upPwdName" method="post" action="/user/upPwdName"><!-- method="post" action="/user/upPwdName"-->
    用户名：<input type="text" name="name" required><span></span><br/>
    <!--<button id="obtain">获取修改密码链接</button>-->
    <input type="submit" value="获取修改密码链接">
</form>
<form method="post" action="/user/upPwdEmail" id="upPwdEmail" style="display: none">
    邮箱：<input type="email" name="email"><span></span><br/>
    验证码：<input type="text" name="verify"><input type="button" id="verificationCode" value="获取验证码"><span></span><br/>
    <input type="submit" value="修改密码"/>
</form>
</body>
</html>